package com.faradel.pharmapp.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.apache.commons.lang.StringUtils;
import org.faradel.pharmapp.entities.Medication;
import org.faradel.pharmapp.entities.PharmaUser;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.faradel.pharmapp.service.MedicationServiceInterface;

@ManagedBean
@ViewScoped
public class MedicationBean implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Resource
    private Medication medication;

    private String action;

    private String medicationId;
    private Map<String, String> searchMap = new HashMap<String, String>();

    private boolean listReturned;

    private List<Medication> medicationList;
    private int index;

    private PharmaUser currentUser;

    {
        medication = getMedicationInstance();

        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
        setCurrentUser((PharmaUser) extContext.getSessionMap().get("sessionUser"));

    }

    private MedicationServiceInterface getMedicationServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (MedicationServiceInterface) context.getBean("medicationServiceImpl");
    }

    private Medication getMedicationInstance()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (Medication) context.getBean("medication");
    }

    public void createMedication()
    {
        medication.setLastModified(TimeClass.getCurrentDate());
        medication.setUserLastModified(currentUser.getPharmaUserId());
        getMedicationServiceInterface().addMedication(medication);
    }

    public void updateMedication()
    {
        medication.setLastModified(TimeClass.getCurrentDate());
        medication.setUserLastModified(currentUser.getPharmaUserId());
        getMedicationServiceInterface().updateMedication(medication);
    }

    public void deleteMedication()
    {
        getMedicationServiceInterface().deleteMedication(medication);
    }

    public void findMedicationById()
    {
        medication = getMedicationServiceInterface().getMedicationById(Integer.parseInt(medicationId));
    }

    public void next()
    {
        try
        {
            medication = medicationList.get(++index);
        }
        catch (IndexOutOfBoundsException ex)
        {
            index = 0;
            medication = medicationList.get(index);
        }

    }

    public void previous()
    {
        try
        {
            medication = medicationList.get(--index);
        }
        catch (IndexOutOfBoundsException ex)
        {
            index = medicationList.size();
            medication = medicationList.get(--index);
        }
    }

    public void findMedicationByColumns()
    {
        medicationList = new ArrayList<Medication>();
        searchMap = checkIfEmpty(medication);

        if (searchMap.isEmpty())
        {
            medicationList = getMedicationServiceInterface().getAllMedications();
            medication = medicationList.get(0);
        }
        else
        {
            medicationList = getMedicationServiceInterface().getMedicationByColumns(searchMap);
            medication = medicationList.get(0);
        }

        if (medicationList.size() > 0)
            listReturned = true;

        index = 0;
    }

    public void reset()
    {
        medication = null;
    }

    private Map<String, String> checkIfEmpty(Medication med)
    {
        Map<String, String> map = new HashMap<String, String>();

        if (StringUtils.isNotBlank(med.getDosageForm()))
        {
            map.put("dosage_form", med.getDosageForm());
        }
        if (StringUtils.isNotBlank(med.getUpcCode()))
        {
            map.put("upc_code", med.getUpcCode());
        }
        if (StringUtils.isNotBlank(med.getGenericName()))
        {
            map.put("generic_name", med.getGenericName());
        }
        if (StringUtils.isNotBlank(med.getMedicationName()))
        {
            map.put("medication_name", med.getGenericName());
        }
        if (StringUtils.isNotBlank(med.getMedicationClass()))
        {
            map.put("medication_class", med.getMedicationClass());
        }
        if (StringUtils.isNotBlank(med.getMedicationStrength()))
        {
            map.put("medication_strength", med.getMedicationStrength());
        }

        return map;
    }

    public Medication getMedication()
    {
        return medication;
    }

    public void setMedication(Medication medication)
    {
        this.medication = medication;
    }

    public List<Medication> getMedicationList()
    {
        return medicationList;
    }

    public void setMedicationList(List<Medication> medicationList)
    {
        this.medicationList = medicationList;
    }

    public String getMedicationId()
    {
        return medicationId;
    }

    public void setMedicationId(String medicationId)
    {
        this.medicationId = medicationId;
    }

    public Map<String, String> getSearchMap()
    {
        return searchMap;
    }

    public void setSearchMap(Map<String, String> searchMap)
    {
        this.searchMap = searchMap;
    }

    public boolean isListReturned()
    {
        return listReturned;
    }

    public void setListReturned(boolean listReturned)
    {
        this.listReturned = listReturned;
    }

    public int getIndex()
    {
        return index;
    }

    public void setIndex(int index)
    {
        this.index = index;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }

    public PharmaUser getCurrentUser()
    {
        return currentUser;
    }

    public void setCurrentUser(PharmaUser currentUser)
    {
        this.currentUser = currentUser;
    }
}
